package sis.studentinfotest;/*
 * @auther 顶风少年
 * @mail dfsn19970313@foxmail.com
 * @date 2019-07-23 18:14
 * @notify
 * @version 1.0
 */

import junit.framework.TestCase;
import sis.studentinfo.Course;
import sis.studentinfo.Session;
import sis.studentinfo.SessionException;
import sis.studentinfo.Student;

import java.net.MalformedURLException;
import java.util.*;

abstract public class SessionTest extends TestCase {
    private Session session;
    private Date startDate;
    public static final int CREDITS = 3;

    public void setUp() {
        startDate = createDate(2003, 1, 6);
        session = createSession(new Course("ENGL","101"), startDate);
        session.setNumberOfCredits(CREDITS);
    }

    public  Date createDate(int year, int month, int day){
        GregorianCalendar gregorianCalendar = new GregorianCalendar();
        gregorianCalendar.add(Calendar.YEAR,year);
        gregorianCalendar.add(Calendar.MONTH,month);
        gregorianCalendar.add(Calendar.DAY_OF_MONTH,day);
        return gregorianCalendar.getTime();
    }


    abstract protected Session createSession(Course course, Date startDate);

    public void testCreate() {
        assertEquals("ENGL", session.getDepartment());
        assertEquals("101", session.getNumber());
        assertEquals(0, session.getNumberOfStudents());
    }
    public void testEnrollStudents() {
        Student student1 = new Student("Cain Diveoe");
        session.enroll(student1);
        assertEquals(CREDITS, student1.getCredits());
        assertEquals(1, session.getNumberOfStudents());
        assertEquals(student1, session.get(0));

        Student student2 = new Student("Cain Diveoe");
        session.enroll(student2);
        assertEquals(CREDITS, student2.getCredits());
        assertEquals(2, session.getNumberOfStudents());
        assertEquals(student1, session.get(0));
        assertEquals(student2, session.get(1));
    }
    public void testComparable(){
        final  Date date = new Date();
        Session sessionA = createSession(new Course("CMSC","101"),date);
        Session sessionB = createSession(new Course("ENGL","101"),date);
        assertTrue(sessionA.compareTo(sessionB)<0);
        assertTrue(sessionB.compareTo(sessionA)>0);

        Session sessionC = createSession(new Course("CMSC","101"),date);
        assertEquals(0,sessionA.compareTo(sessionC));

        Session sessionD = createSession(new Course("CMSC","210"),date);
        assertTrue(sessionC.compareTo(sessionD)<0);
        assertTrue(sessionD.compareTo(sessionC)>0);
    }

    public void testAverageGpaForParTimeStudents(){
        //及格
        session.enroll(createFullTimeStudent());

        //不及格
        Student partTimer1 = new Student("1");
        partTimer1.addGrade(Student.Grade.A);
        session.enroll(partTimer1);

        //及格
        session.enroll(createFullTimeStudent());

        //不及格
        Student partTimer2 = new Student("2");
        partTimer2.addGrade(Student.Grade.B);
        session.enroll(partTimer2);

        assertEquals(3.5,session.averageGpaForPartTimeStudents(),0.05);

    }
    //创建一个学分合格的学生
    private Student createFullTimeStudent(){
        Student student = new Student("a");
        student.addCredits(Student.CREDITS_REQUIRED_FOR_FULL_TIME);
        return student;
    }

    public void testIterate(){
        enrollStudents(session);
        List<Student> results = new ArrayList<Student>();
        for(Student student:session){
            results.add(student);
        }
        assertEquals(session.getAllStudents(),results);
    }
    private void  enrollStudents(Session session){
        session.enroll(new Student("1"));
        session.enroll(new Student("2"));
        session.enroll(new Student("3"));
    }

    public void testSessionUrl() throws SessionException {
        final String url = "http://course.langrsoft.com/cmsc300";
        session.setUrl(url);
        assertEquals(url,session.getUrl().toString());
    }

    public void testInvalidSessionUrl(){
        final String url = "httpd://course.langrsoft.com/cmsc300";
        try {
            session.setUrl(url);
        }catch (SessionException e){
            Throwable cause = e.getCause();
            assertEquals(MalformedURLException.class,cause.getClass());
        }
    }
}
